    function [Ppaid Ppaid_mean Switch_mean dead_mean Stay] = Paid_prices(Pspot,iX)

    Nstates = size(Pspot,1)-1;
    T = size(iX,1);
    nInd = size(iX,2);
    Ps = Pspot(:,1);
    Psi = Ps(iX(1,:))';

    Pgri = Psi;
    Ppaid = zeros(T,nInd);
    Ppaid(1,:) =  Psi;
    Stay = ones(T,nInd);

    for t = 2:T
       Ps = Pspot(:,t);
       Psi = Ps(iX(t,:))'; 
       % drop if dead:
       stay = (Pgri<=Psi).*(iX(t,:)~=(Nstates+1));
       Pgri = Pgri.*stay + Psi.*(1-stay);
       Ppaid(t,:) = Pgri; 
       Stay(t,:) = stay;
    end

% Ppaid has 0s for those in state 8
Ppaid_mean = sum(Ppaid,2)./sum(iX~=(Nstates+1),2); 
% do not stay among 
Switch_mean = sum((1-Stay).*(iX~=(Nstates+1)),2)./sum(iX~=(Nstates+1),2);
% dead mean
dead_mean = sum(iX==(Nstates+1),2)/nInd;



end